package com.zhkj.shixi.tvfilemanager;


import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.provider.MediaStore;
import android.util.Log;

import com.zhkj.shixi.tvfilemanager.Interface.IFileManager;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FileManager implements IFileManager {
    public static final int MUSIC_TYPE = 0;
    public static final int VIDEO_TYPE = 1;

    public int getmUSB() {
        return mUSB;
    }

    public void setmUSB(int mUSB) {
        this.mUSB = mUSB;
    }

    public static final int IMAGE_TYPE = 2;

    private String mCurrentURL;
    private int mUSB;
    private int mType=-1;
    @Override
    public int getType() {
        return mType;
    }

    public void setType(int type) {
        this.mType = type;
    }

    @Override
    public String getCurrentURL() {
        return mCurrentURL;
    }

    public void setCurrentURL(String currentURL) {
        this.mCurrentURL = currentURL;
    }

    public static List<File> getAllFiles(String path)
    {
        File f = new File(path);
        List<File> fileList = Arrays.asList(f.listFiles());
        if(fileList == null) Log.e("File","没有文件");
        return fileList;
    }

    public static List<File> getTypeFiles(String path,int type){

        if(type > 2 || type < 0)throw new RuntimeException("不支持的文件格式");
        File f = new File(path);

        List<File> fileList = Arrays.asList(f.listFiles(new typeFilter(type,false)));

        if(fileList == null) Log.e("File","没有文件");
        return fileList;
    }

    public static  List<File> getTypeFilesWithNoDir(String path,int type){
        if(type > 2 || type < 0)throw new RuntimeException("不支持的文件格式");
        File f = new File(path);

        List<File> fileList = Arrays.asList(f.listFiles(new typeFilter(type,true)));

        if(fileList == null) Log.e("File","没有文件");

        return fileList;
    }
    private static class typeFilter implements FileFilter
    {
        private boolean wnd;
        private List<String> anyType = new ArrayList<>();
        public typeFilter(int type,boolean wnd) {
            this.wnd=wnd;
            if(type == FileManager.MUSIC_TYPE){
                final List<String> musicType =  new ArrayList<>();
                musicType.add(".mp3");
                musicType.add(".wav");
                musicType.add(".ogg");
                musicType.add(".ape");
                musicType.add(".flac");
                anyType = musicType;
            }

            else if(type == FileManager.VIDEO_TYPE) {
                final List<String> videoType =  new ArrayList<>();
                videoType.add(".mp4");
                videoType.add(".3gp");
                videoType.add(".wmv");
                videoType.add(".ts");
                videoType.add(".rmvb");
                videoType.add(".mov");
                videoType.add(".m4v");
                videoType.add(".avi");
                videoType.add(".m3u8");
                videoType.add(".3gpp");
                videoType.add(".3gpp2");
                videoType.add(".mkv");
                videoType.add(".flv");
                videoType.add(".divx");
                videoType.add(".f4v");
                videoType.add(".rm");
                videoType.add(".asf");
                videoType.add(".ram");
                videoType.add(".mpg");
                videoType.add(".v8");;
                videoType.add(".swf");
                videoType.add(".m2v");
                videoType.add(".asx");
                videoType.add(".ra");
                videoType.add(".ndivx");
                videoType.add(".xvid");
                anyType = videoType;
            }

            else if(type == FileManager.IMAGE_TYPE){
                final List<String> imageType =  new ArrayList<>();
                imageType.add(".png");
                imageType.add(".jpg");
                imageType.add(".jpeg");

                imageType.add(".gif");
                imageType.add(".webp");
                imageType.add(".ico");
                anyType = imageType;
            }
        }
        public boolean accept(File file) {
            for(int i = 0;i < anyType.size();i++) {
                if(file.getName().toLowerCase().endsWith(anyType.get(i))){
                    return true;
                }
                else if(file.isDirectory()) {
                    if (this.wnd)
                        return false;//过滤文件夹
                    else
                        return true;
                }
            }
            return false;
        }
    }

    /**
     * 获取屏幕高度(px)
     */
    public static int getScreenHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }
    /**
     * 获取屏幕宽度(px)
     */
    public static int getScreenWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

    public static Bitmap getImageThumbnail(String imagePath, int width, int height) {
        Bitmap bitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        // 获取这个图片的宽和高，注意此处的bitmap为null
        bitmap = BitmapFactory.decodeFile(imagePath, options);
        options.inJustDecodeBounds = false; // 设为 false
        // 计算缩放比
        int h = options.outHeight;
        Log.i("linjing","options h "+h);
        int w = options.outWidth;
        Log.i("linjing","options  w"+w);
        double beWidth = 1.0* w / width;
        Log.i("linjing","width "+width);
        Log.i("linjing","height "+height);
        double beHeight = 1.0* h / height;
        int be = 1;
        if (beWidth > beHeight) {
            if (beWidth >(int)beWidth && beWidth < (int)beWidth+1) {
                be = (int)beWidth+1;
            }else {
                be = (int)beWidth;
            }
        } else {
            if (beHeight >(int)beHeight && beHeight < (int)beHeight+1) {
                be = (int)beHeight+1;
            }else {
                be = (int)beHeight;
            }
        }
        if (be <= 0) {
            be = 1;
        }

        options.inSampleSize = be;
        // 重新读入图片，读取缩放后的bitmap，注意这次要把options.inJustDecodeBounds 设为 false
        bitmap = BitmapFactory.decodeFile(imagePath, options);
        // 利用ThumbnailUtils来创建缩略图，这里要指定要缩放哪个Bitmap对象
        bitmap = ThumbnailUtils.extractThumbnail(bitmap, w/be, h/be,
                ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        return bitmap;
    }



}
